<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.4/css/all.min.css" integrity="sha256-mUZM63G8m73Mcidfrv5E+Y61y7a12O5mW4ezU3bxqW4=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"tallgy.gitee.io","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>
<meta name="description" content="router3 源码初学​        最近在学vue-router，然后在学习的过程中，顺便简单的翻阅翻阅源码来看看一些问题。后续也会认真的查看源码部分。 ​        这个是 vue2 的部分。 beforeRouteEnter​        在 beforeRouteEnter 中，我们发现如果在创建方法的时候创建了回调函数 ​        我们发现如果定义了形参，那么就需要调用，">
<meta property="og:type" content="article">
<meta property="og:title" content="router3源码初学">
<meta property="og:url" content="http://tallgy.gitee.io/2022/02/11/router/%E6%BA%90%E7%A0%81/router3%E6%BA%90%E7%A0%81/index.html">
<meta property="og:site_name" content="tallgy&#39;s blog">
<meta property="og:description" content="router3 源码初学​        最近在学vue-router，然后在学习的过程中，顺便简单的翻阅翻阅源码来看看一些问题。后续也会认真的查看源码部分。 ​        这个是 vue2 的部分。 beforeRouteEnter​        在 beforeRouteEnter 中，我们发现如果在创建方法的时候创建了回调函数 ​        我们发现如果定义了形参，那么就需要调用，">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://tallgy.gitee.io/.io//router3%E6%BA%90%E7%A0%81%5Cimage-20220211173002676.png">
<meta property="og:image" content="http://tallgy.gitee.io/.io//image-20220211173238506.png">
<meta property="og:image" content="http://tallgy.gitee.io/.io//image-20220211175019270.png">
<meta property="og:image" content="http://tallgy.gitee.io/.io//image-20220211184544135.png">
<meta property="og:image" content="http://tallgy.gitee.io/.io//image-20220211190158819.png">
<meta property="og:image" content="http://tallgy.gitee.io/.io//image-20220211173522407.png">
<meta property="og:image" content="http://tallgy.gitee.io/.io//image-20220211174408513.png">
<meta property="og:image" content="http://tallgy.gitee.io/.io//image-20220211175554195.png">
<meta property="og:image" content="http://tallgy.gitee.io/.io//image-20220211175816841.png">
<meta property="og:image" content="http://tallgy.gitee.io/.io//image-20220211180402862.png">
<meta property="og:image" content="http://tallgy.gitee.io/.io//image-20220211185433641.png">
<meta property="og:image" content="http://tallgy.gitee.io/.io//image-20220211190859440.png">
<meta property="og:image" content="http://tallgy.gitee.io/.io//image-20220211191050870.png">
<meta property="article:published_time" content="2022-02-11T08:00:22.000Z">
<meta property="article:modified_time" content="2023-06-25T08:57:29.323Z">
<meta property="article:author" content="tallgy">
<meta property="article:tag" content="router">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://tallgy.gitee.io/.io//router3%E6%BA%90%E7%A0%81%5Cimage-20220211173002676.png">


<link rel="canonical" href="http://tallgy.gitee.io/2022/02/11/router/%E6%BA%90%E7%A0%81/router3%E6%BA%90%E7%A0%81/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://tallgy.gitee.io/2022/02/11/router/%E6%BA%90%E7%A0%81/router3%E6%BA%90%E7%A0%81/","path":"2022/02/11/router/源码/router3源码/","title":"router3源码初学"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>router3源码初学 | tallgy's blog</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">tallgy's blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>







</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#router3-%E6%BA%90%E7%A0%81%E5%88%9D%E5%AD%A6"><span class="nav-number">1.</span> <span class="nav-text">router3 源码初学</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#beforeRouteEnter"><span class="nav-number">1.1.</span> <span class="nav-text">beforeRouteEnter</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">tallgy</p>
  <div class="site-description" itemprop="description">只是一个知识的搬运工</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">107</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">32</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">74</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://tallgy.gitee.io/2022/02/11/router/%E6%BA%90%E7%A0%81/router3%E6%BA%90%E7%A0%81/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="tallgy">
      <meta itemprop="description" content="只是一个知识的搬运工">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="tallgy's blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          router3源码初学
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-02-11 16:00:22" itemprop="dateCreated datePublished" datetime="2022-02-11T16:00:22+08:00">2022-02-11</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2023-06-25 16:57:29" itemprop="dateModified" datetime="2023-06-25T16:57:29+08:00">2023-06-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/router/" itemprop="url" rel="index"><span itemprop="name">router</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="router3-源码初学"><a href="#router3-源码初学" class="headerlink" title="router3 源码初学"></a>router3 源码初学</h1><p>​        最近在学vue-router，然后在学习的过程中，顺便简单的翻阅翻阅源码来看看一些问题。后续也会认真的查看源码部分。</p>
<p>​        这个是 vue2 的部分。</p>
<h2 id="beforeRouteEnter"><a href="#beforeRouteEnter" class="headerlink" title="beforeRouteEnter"></a>beforeRouteEnter</h2><p>​        在 beforeRouteEnter 中，我们发现如果在创建方法的时候创建了回调函数</p>
<p>​        我们发现如果定义了形参，那么就需要调用，否则就会无法跳转。这就很奇怪。</p>
<p>​        所以我现在先看看代码，因为是第一次看，所以也有很多看不懂，所以会猜测。</p>
<p>先，在src目录里面搜寻 <strong>beforeRouteEnter</strong> ，然后查看 src/history/base 里面存在这个。<strong>extractEnterGuards</strong></p>
<p><img src="/.io//router3%E6%BA%90%E7%A0%81%5Cimage-20220211173002676.png" alt="image-20220211173002676"></p>
<p>​        可以发现，是在这里进行了使用。这个函数就是通过传递一个<strong>activated</strong>数组。然后调用 <strong>extractGuards</strong> 。这里activated就是路由匹配后，会处于routeEnter状态的路由数组。</p>
<p>​        <img src="/.io//image-20220211173238506.png" alt="image-20220211173238506"></p>
<p>​        这个函数就是创建 guards，获取<strong>flatMapComponents</strong> 返回的数据。然后再调用<strong>flatten</strong> 作为返回值。</p>
<p>​        <strong>flatMapComponents</strong> 方法就是将每个matched里面的数组元素进行传递给 <strong>flatten</strong> </p>
<p>其中，是从m.componets 里面获取key，然后循环，拿出 自己的 组件，实例，和m，key。然后传递给fn 进行调用，然后将返回值作为一个数组，然后循环了matched的length那么多次，最后返回给了 <strong>flatten</strong> 进行了合并。</p>
<p><img src="/.io//image-20220211175019270.png" alt="image-20220211175019270"> </p>
<p>​        <strong>flatten</strong> 合并数组，扁平化处理。</p>
<p>这个是 route.matched 的路由数组</p>
<img src="/.io//image-20220211184544135.png" alt="image-20220211184544135" style="zoom:67%;">

<p>这里传递的参数是，先 matched.map(m =&gt; {})，先取出匹配上的路由。进行循环。然后将路由里面的 m.components 就是获取路由里面的components，默认就是一个 default，然后就是map循环路由里面的components。然后取出components。instances，路由本身。key（就是组件名字。）</p>
<img src="/.io//image-20220211190158819.png" alt="image-20220211190158819" style="zoom:67%;">



<p><img src="/.io//image-20220211173522407.png" alt="image-20220211173522407"></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[1, 2], [2, 3] =&gt; [1, 2, 2, 3]</span><br></pre></td></tr></table></figure>

<p><strong>flatMapComponents</strong> 的fn是传递的方法</p>
<p><img src="/.io//image-20220211174408513.png" alt="image-20220211174408513"></p>
<p>​        <strong>guard</strong>：就是调用这个 extractGuard 的返回值，参数分别为 def：m.components[key] 和 name beforeRouteEnter。</p>
<p><img src="/.io//image-20220211175554195.png" alt="image-20220211175554195"></p>
<p>​        这个方法，先判断 def 是不是一个函数。然后对于不是函数的，调用 Vue.extend 方法。这个方法通过查看 API 可以知道是。</p>
<img src="/.io//image-20220211175816841.png" alt="image-20220211175816841" style="zoom:67%;">

<p>​        简单来看，就像是，先将def进行了extend，然后拿出里面的 options 的 beforeRouteEnter 参数。就是说从组件里面取出beforeRouteEnter方法。</p>
<img src="/.io//image-20220211180402862.png" alt="image-20220211180402862" style="zoom:67%;">

<p>​        所以，知道了 guard 是返回一个 自己定义的 <strong>beforeRouteEnter</strong> 方法。同时 <strong>Vue.extend(def).options[‘beforeRouteEnter’]</strong> 返回的是一个数组（这里我认为应该是Vue的处理，方便混入或者其他情况的解决。但是对于源码我还没有了解。所以不怎么清楚）。</p>
<p>​        然后继续进入 <strong>flatMapComponents</strong> 查看 322 行。进行bind方法的调用（bind 函数是<strong>extractGuards</strong> 传递过来的一个方法）。</p>
<p><img src="/.io//image-20220211185433641.png" alt="image-20220211185433641"></p>
<p>​        通过查看 extractEnterGuards 可以发现，这个会调用bind，然后return 一个 bindEnterGuard。</p>
<p><img src="/.io//image-20220211190859440.png" alt="image-20220211190859440"></p>
<p>​        我们可以查看 bindEnterGuard 方法。来查看里面的跳转情况。</p>
<p>​        这个方法就是返回一个 routeEnterGuard 方法。然后在方法里面会返回一个guard的调用的返回值。这里guard 就是对应的 <strong>beforeRouteEnter</strong> 方法。</p>
<p>​        通过这个方法我们可以知道，<strong>routeEnterGuard</strong> 里面 guard 是我们自定的 <strong>beforeRouteEnter</strong> 方法。里面的参数中，第三个参数的箭头函数，就是代表了我们的 <strong>beforeRouteEnter</strong> 方法中的 next。同时我们如果输出next也可以发现是正确的。</p>
<p>​        所以我们在 beforeRouteEnter 里面调用next() ，其实就是调用这个方法。然后如果我们传递了参数，那么我们就会进行里面的操作。</p>
<img src="/.io//image-20220211191050870.png" alt="image-20220211191050870" style="zoom:67%;">

<p>​        同时我们发现，如果我们不调用next方法。这个 guard方法是不会调用的。</p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/router/" rel="tag"># router</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2022/02/10/router/router%E8%BF%9B%E9%98%B6/" rel="prev" title="router进阶">
                  <i class="fa fa-chevron-left"></i> router进阶
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2022/02/11/router/%E6%BA%90%E7%A0%81/router4/router4%E6%BA%90%E7%A0%81/" rel="next" title="router4源码">
                  router4源码 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">tallgy</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  





  





</body>
</html>
